public class MyLinkedList implements ILinkedList{

    static class ListNode{
        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    ListNode head;
    ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = last = node;
            return;
        }

        node.next = head;
        head.prev = node;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            addFirst(data);
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }


    private boolean checkPos(int index){
        if(index < 0 || index > size()){
            return false;
        }
        return true;
    }


    @Override
    public void addIndex(int index, int data) {
       if (!checkPos(index)){
           System.out.println("位置不合理");
           return;
       }
       //头插
       if(index == 0){
           addFirst(data);
           return;
       }
       //尾插
        if (index == size()){
            addLast(data);
            return;
        }
        //中间插
        ListNode cur = head;
        while (index > 1){
            cur = cur.next;
            index--;
        }
        ListNode curN = cur.next;
        ListNode node = new ListNode(data);
        node.next = curN;
        cur.next = node;
        node.prev = cur;
        curN.prev = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        //头删
        if(head.val == key){
            head = head.next;
            head.prev = null;
            return;
        }
        //未删
        if(last.val == key){
            last = last.prev;
            last.next = null;
            return;
        }
        //中间删除
        while(cur != null){
            if (cur.next.val == key){
                cur.next = cur.next.next;
                cur.next.next.prev = cur;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        /*ListNode curN = head.next;
        while (curN.next != null){
            if(curN.val == key){
                cur.next = curN.next;
                curN.next.prev = cur;
                curN = curN.next;
            }else {
                cur = curN;
                curN = curN.next;
            }
        }

        if (head.val == key){
            head = head.next;
            head.prev = null;
        }
        if (last.val == key){
            last = last.prev;
            last.next = null;
        }*/
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head.next != null){
                        head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if (cur.next == null){
                        last = last.prev;
                        last.next = null;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int len = 0;
        while (cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();

    }

    @Override
    public void clear() {
        /*ListNode cur = head.next;
        while (cur != null){
            head.next = null;
            head.prev = null;

            head = cur;
            cur = cur.next;
        }
        head = last = null;*/

        ListNode cur = head;
        while (cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curN;
        }
        head = last = null;
    }
}
